<?php 
class EComUtils {
  public static function update_alert($orm, $product, $objs) {
    if (count($objs) == 0) {
      $product->alert__ = ($product->stock <= $product->stock_alert) ? 1 : 0;
      $orm->update('pb_ecom_product', $product);
    } else {
      $product->stock = 0;
      $alert = 0;
      foreach ($objs as $itm) {
        $product->stock += $itm->stock;
        if ($itm->stock <= $itm->stock_alert && $itm->stock_alert != 0) {
          $alert = 1;
        }
      }
      $product->alert__ = $alert;
      $orm->update('pb_ecom_product', $product);
    }
  }
  
  public static function update_stock($orm, $purchase, $is_cancel) {
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $purchase->product_id);
    $rslt = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($rslt) > 0) {
      if ($rslt[0]->enabled_stock == 1) {
        if ($purchase->attr_opt_id != '') {
          $opts = new stdClass();
          $opts->cd = 't.product_track_id = :v1';
          $opts->cdv = array(':v1' => $rslt[0]->track_id);
          $rslt2 = $orm->get('pb_ecom_product_obj', NULL, NULL, NULL, NULL, $opts)->d;
          $alert = 0;
          foreach ($rslt2 as $itm) {
            if ($itm->attr_opt_id == $purchase->attr_opt_id) {
              if ($is_cancel == 0) {
                $itm->stock -= $purchase->quantity;
              } else {
                $itm->stock += $purchase->quantity;
              }
              if ($is_cancel == 0) {
                $rslt[0]->stock -= $purchase->quantity;
              } else {
                $rslt[0]->stock += $purchase->quantity;
              }
              if ($itm->stock <= $itm->stock_alert && $itm->stock_alert != 0) {
                $alert = 1;
              }
              $orm->update('pb_ecom_product_obj', $itm);
            }
            $rslt[0]->alert__ =  $alert;
          }
        } else {
          if ($is_cancel == 0) {
            $rslt[0]->stock -= $purchase->quantity;
          } else {
            $rslt[0]->stock += $purchase->quantity;
          }
          $rslt[0]->alert__ = ($rslt[0]->stock <= $rslt[0]->stock_alert && $rslt[0]->stock_alert != 0) ? 1 : 0;
        }
      }
      if ($is_cancel == 0) {
        $rslt[0]->sold__ += $purchase->quantity;
      } else {
        $rslt[0]->sold__ -= $purchase->quantity;
      }
      
      $orm->update('pb_ecom_product', $rslt[0]);
    }
  }
  
  public static function check_stock($orm, $purchase) {
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $purchase->product_id);
    $rslt = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($rslt) > 0) {
      if ($rslt[0]->enabled_stock == 1) {
        if ($purchase->attr_opt_id != '') {
          $opts = new stdClass();
          $opts->cd = 't.product_track_id = :v1';
          $opts->cdv = array(':v1' => $rslt[0]->track_id);
          $rslt2 = $orm->get('pb_ecom_product_obj', NULL, NULL, NULL, NULL, $opts)->d;
          foreach ($rslt2 as $itm) {
            if ($itm->attr_opt_id == $purchase->attr_opt_id) {
              $purchase->__stock = $itm->stock;
              if ($purchase->quantity <= $itm->stock) {
                return true;
              }
            }
          }
        } else {
          $purchase->__stock = $rslt[0]->stock;
          if ($purchase->quantity <= $rslt[0]->stock) {
            return true;
          }
        }
      } else {
        return true;
      }
    }
    return false;
  }
  
  public static function update_customer($orm, $order) {
    $rslt = unserialize(CURRS);
    reset($rslt);
    $curr = key($rslt);
    
    $currs = $orm->get('pb_ecom_currency', NULL, NULL, NULL, NULL, NULL)->d;
    foreach ($currs as $itm) {
      if ($itm->title == $curr) {
        $c1 = $itm;
      } 
      if ($itm->title == $order->curr) {
        $c2 = $itm;
      }
      if ($itm->is_default == 1) {
        $c3 = $itm;
      }
    }

    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $order->customer_id);
    $rslt = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($rslt) > 0) {
      if ($order->is_paid == 1 && $order->is_cancel == 0) {
        $rslt[0]->spent__ += ($c2->rate / $c1->rate) * ($order->total + $order->delivery_subtotal);
      } else if ($order->is_paid == 1 && $order->is_cancel == 1) {
        $rslt[0]->spent__ -= ($c2->rate / $c1->rate) * ($order->total + $order->delivery_subtotal);
      }
      
      $members = $orm->get('pb_ecom_membership', 't.total_spent', 'ASC', NULL, NULL, NULL)->d;
      
      $rslt[0]->__discount = 0;
      foreach ($members as $itm) {
        if ($itm->id == $rslt[0]->member_id) {
          $rslt[0]->__discount = $itm->discount;
        }
      }
      
      foreach ($members as $itm) {
        $itm->__total = ($c3->rate / $c1->rate) * $itm->total_spent;
        if ($itm->__total <= $rslt[0]->spent__) {
          if ($itm->discount > $rslt[0]->__discount) {
            $rslt[0]->member_id = $itm->id;
          }
        }
      }
      $orm->update('pb_ecom_customer', $rslt[0]);
    }
  }
}
?>
